package com.icss.controller;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.icss.dao.ItemDao;
import com.icss.dao.RecordDao;
import com.icss.dao.UserDao;
import com.icss.entity.Item;
import com.icss.entity.Record;
import com.icss.entity.User;
import com.icss.service.RecordService;
import com.icss.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller
@Component
public class RecordController {
    public static Logger log = LoggerFactory.getLogger(RecordController.class);
    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private UserDao userDao;
    @RequestMapping("")
    public String index(){
        return "redirect:recordlist";
    }
    @RequestMapping("recordlist")
    public String recordList(ModelMap map,
                             @RequestParam(value = "pageNum",defaultValue = "0") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "4")int pageSize,
                             HttpServletRequest request){
        String carnumber = request.getParameter("carnumber");
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        Page<Record> records = recordService.conditionQuery(pageNum, pageSize, carnumber, start, end);
        System.out.println(records.getTotalPages());
        map.addAttribute("records",records);
        return "recordList";
    }
    //显示所有记录
//    @RequestMapping("selectRecord")
//    public String selectrecord(ModelMap map,
//                                     @RequestParam(value = "pageNum",defaultValue = "0") int pageNum1,
//                                     @RequestParam(value = "pageSize",defaultValue = "4")int pageSize1,
//                                     HttpServletRequest request) throws ServletException, IOException {
//
//
//        String carnumber = request.getParameter("carnumber");
//        String start = request.getParameter("start");
//        String end = request.getParameter("end");
//        Page<Record> records = recordService.conditionQuery(pageNum1, pageSize1, carnumber, start, end);
//        System.out.println(records.getTotalPages());
//        map.addAttribute("records",records);
//        return "recordList";
//    }
    //传record的json数据
    @RequestMapping("showrecord")
    @JsonBackReference
    public ResponseEntity<List<Record>> show(){
        List<Record> record1 = recordService.showRecord();
        for (Object objects : record1) {
            System.out.println(objects.toString());
        }
        return new ResponseEntity<List<Record>>(record1,HttpStatus.OK);
    }
    //传user的json数据
    @RequestMapping("showuser")
    @JsonBackReference
    public ResponseEntity<List<User>> showUser(){
        List<User> users = userService.selectAllUser();
        for (Object objects : users) {
            System.out.println(objects.toString());
        }
        return new ResponseEntity<List<User>>(users,HttpStatus.OK);
    }
    @RequestMapping("record")
    public String showrecord(){
        return "addrecord";
    }
    @RequestMapping("editrecord")
    public String showrecord1(){
        return "editrecord";
    }
    //添加记录
    @RequestMapping("addRecord")
    public String addRecord(@RequestParam(value = "illegalTime")String illegalTime,Record record,HttpServletRequest request){//,
        String userName = request.getParameter("userName");
        System.out.println(userName);
        Integer userId = recordDao.findByusername(userName);
        User user = userService.selectUser(userId);
        System.out.println(user);
        String illegalBehavior = request.getParameter("illegalBehavior");
        Item item = itemDao.findItemByillegalBehavior(illegalBehavior);
        System.out.println(item);
        Timestamp timestamp = Timestamp.valueOf(illegalTime);
        System.out.println(timestamp);
        String carNumber = request.getParameter("carNumber");
        System.out.println(carNumber);
        String illegalSite = request.getParameter("illegalSite");
        Integer state=0;
        System.out.println(state);
        record.setUser(user);
        record.setItem(item);
        record.setState(state);
        record.setIllegalSite(illegalSite);
        record.setCarNumber(carNumber);
        record.setIllegalTime(timestamp);
        recordService.addRecord(record);
        return "redirect:recordlist";
    }
    @RequestMapping("dealrecord")
    public String dealrecord(@RequestParam(value = "penalty") Integer penalty,@RequestParam(value = "score") Integer score,@RequestParam(value = "userid") Integer userid){
        System.out.println(penalty);
        System.out.println(score);
        System.out.println(userid);
        recordService.manageRecord(penalty,score,userid);
        return "redirect:recordlist";
    }
    @RequestMapping("editRecord")
    public String editrecord(Integer id, ModelMap map){
        Record record = recordDao.findByid(id);
        System.out.println(record.toString());
        map.addAttribute("result",record);
        return "editrecord";
    }
    @RequestMapping("editRecord1")
    public String editrecord1(String userName,Integer tel,Integer userId){
        System.out.println("用户id：" +userId+"用户姓名："+userName+"用户手机号："+tel);
        recordDao.updateRecord(userName,tel,userId);
        return "redirect:recordlist";
    }
}
